package com.shujia.bigdata.student;

import java.io.*;
import java.util.HashMap;
import java.util.Hashtable;

public class Map {
    public static void main(String[] args) {

        /**
         * 通过多行程处理多个文件
         *
         */


        File pfile = new File("java/data/student/");
        File[] files = pfile.listFiles();

        if (files == null) {
            return;
        }

        for (int i = 0; i < files.length; i++) {
            Task task = new Task(files[i], i);
            task.start();
        }

    }
}

class Task extends Thread {

    private File file;
    private Integer partition;

    public Task(File file, int partition) {
        this.file = file;
        this.partition = partition;
    }

    @Override
    public void run() {


        try {
            /**
             * 统计每个文件每个班级的人数
             *
             */
            FileReader fileReader = new FileReader(file);
            BufferedReader bufferedReader = new BufferedReader(fileReader);

            HashMap<String, Integer> map = new HashMap<>();
            String line;

            while ((line = bufferedReader.readLine()) != null) {

                //通过都好分割数据，取出班级
                String[] split = line.split(",");
                String clazz = split[4];


                //如果map集合里面不存在这个班级，直接put,
                //如果存在，在之前统计的结果是加1

                Integer integer = map.get(clazz);//如果key不存在返回null

                if (integer == null) {
                    map.put(clazz, 1);
                } else {
                    map.put(clazz, integer + 1);
                }


            }

            /**
             * 每个task处理之后将结果写入磁盘
             *
             */

            FileWriter fileWriter = new FileWriter("java/data/stu_tmp/" + partition);

            BufferedWriter writer = new BufferedWriter(fileWriter);

            for (String clazz : map.keySet()) {
                Integer count = map.get(clazz);

                String kv = clazz + "," + count;

                writer.write(kv);
                writer.newLine();

            }

            writer.close();

            bufferedReader.close();


        } catch (Exception e) {
            e.printStackTrace();
        }


    }
}
